package com.cirkana.viewer.comm;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.location.Location;

import com.cirkana.Global;
import com.cirkana.media.MediaPlayer;
import com.cirkana.viewer.game.MemoryGameActivity;
import com.cirkana.viewer.route.model.gpx.Track;
import com.cirkana.viewer.route.model.gpx.TrackPoint;
import com.cirkana.viewer.route.model.gpx.Tracker;
import com.cirkana.viewer.route.model.gpx.WayPoint;

public class Communicator {

	public static final String MESSAGE_NO_MESSAGE = "noMessage";
	public static final String MESSAGE_CHANGE_LOCATION = "changeLocation";
	public static final String MESSAGE_SET_WAYPOINT = "setWaypoint";
	public static final String MESSAGE_START_TRACK = "startTrack";
	public static final String MESSAGE_SET_TRACKPOINT = "setTrackpoint";
	public static final String MESSAGE_FINISH_TRACK = "finishTrack";

	public static final String CONTENT_LATITUDE = "latitude";
	public static final String CONTENT_LONGITUDE = "longitude";
	public static final String CONTENT_WAYPOINT_CONTENTS = "waypointContents";

	List<Message> messages = new ArrayList<Message>();
	Message currentMessage = null;

	public String nextMessage() {
		if (this.messages.size() > 0) {
			this.currentMessage = this.messages.remove(0);
			return this.currentMessage.getType();
		} else
			return MESSAGE_NO_MESSAGE;
	}

	public String getLat() {
		return this.currentMessage.getContent(CONTENT_LATITUDE);
	}

	public String getLon() {
		return this.currentMessage.getContent(CONTENT_LONGITUDE);
	}

	public String getWayPointContents() {
		return this.currentMessage.getContent(CONTENT_WAYPOINT_CONTENTS);
	}

	protected class Message {
		private String type;
		private Hashtable<String, String> contents;

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getContent(String content) {
			return this.contents.get(content);
		}

		protected Message(String type, Hashtable<String, String> contents) {
			this.type = type;
			this.contents = contents;
		}
	}

	public void addChangeLocationMessage(Location location) {
		Hashtable<String, String> contents = new Hashtable<String, String>();
		contents.put(CONTENT_LATITUDE, Double.toString(location.getLatitude()));
		contents.put(CONTENT_LONGITUDE, Double
				.toString(location.getLongitude()));
		this.messages.add(new Message(MESSAGE_CHANGE_LOCATION, contents));
	}

	public void addTrackerMessages(Tracker tracker) {
		for (WayPoint wayPoint : tracker.getWayPoints())
			addWayPointMessage(wayPoint);
		for (Track track : tracker.getTracks())
			addTrackMessages(track);
	}

	public void addTrackMessages(Track track) {
		addStartTrackMessage(track);
		for (TrackPoint trackPoint : track.getTrackPoints())
			addTrackPointMessage(trackPoint);
		addFinishTrackMessage(track);
	}

	public void addStartTrackMessage(Track track) {
		this.messages.add(new Message(MESSAGE_START_TRACK, null));
	}

	public void addTrackPointMessage(TrackPoint trackPoint) {
		Hashtable<String, String> contents = new Hashtable<String, String>();
		contents.put(CONTENT_LATITUDE, Double.toString(trackPoint.getLat()));
		contents.put(CONTENT_LONGITUDE, Double.toString(trackPoint.getLon()));
		this.messages.add(new Message(MESSAGE_SET_TRACKPOINT, contents));
	}

	public void addFinishTrackMessage(Track track) {
		this.messages.add(new Message(MESSAGE_FINISH_TRACK, null));
	}

	public void addWayPointMessage(WayPoint wayPoint) {
		Hashtable<String, String> contents = new Hashtable<String, String>();
		contents.put(CONTENT_LATITUDE, Double.toString(wayPoint.getLat()));
		contents.put(CONTENT_LONGITUDE, Double.toString(wayPoint.getLon()));
		contents.put(CONTENT_WAYPOINT_CONTENTS, buildWayPointHtmlContents(
				wayPoint.getName(), wayPoint.getDesc(), wayPoint.getCmt()));
		this.messages.add(new Message(MESSAGE_SET_WAYPOINT, contents));
	}

	public void playMedia(String type, String url) {
		MediaPlayer.play(url);
	}

	public void playGame(String type) {
		Global.getCurrentActivity().startActivity(
				MemoryGameActivity.createIntent(Global.getCurrentActivity()));
	}

	private String buildWayPointHtmlContents(String title, String description,
			String comments) {
		StringBuilder s = new StringBuilder();
		s.append("<b>");
		s.append(title);
		s.append("</b><p/>");
		s.append(description);
		s.append("<p/>");
		s.append(comments);
		return s.toString();
	}

}
